import {createStore, combineReducers, applyMiddleware,compose} from 'redux'
// redux 中间件
import {thunk} from 'redux-thunk';
import promise from "redux-promise";
// 数据持久化
import {persistStore, persistReducer} from "redux-persist";
import storage from 'redux-persist/lib/storage'

// 本地reducer
import global from '@/redux/modules/global/reducer'
import menuList from  '@/redux/modules/Menu/reducer'
import subData from '@/redux/modules/subModule/reducer'


// 创建reducer(拆分reducer)
// @ts-ignore
const reducer = combineReducers({
     global,
     menuList,
     subData,
})

// 数据持久化:是指页面刷新后，数据仍然能够保持原来的状态
const persistConfig = {
    key: 'rootStorage',
    storage,
    whitelist: ['global'] // 设置某个reducer数据持久化.whitelist:白名单；blacklist:黑名单
}

// @ts-ignore
const persistedReducer = persistReducer(persistConfig, reducer);

// 开启 redux-devtools
// @ts-ignore
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

// 使用 redux 中间件
const middleWares = applyMiddleware(thunk, promise);

// 创建 store
const store = createStore(persistedReducer, composeEnhancers(middleWares));

// 创建持久化 store
const persistor = persistStore(store);

export { store, persistor };
